import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;


public class MainFrame extends JFrame {
	
	private JMenuBar menuBar;
	private JMenu gameMenu, helpMenu, profileMenu;
	private JMenuItem about, logOut, returnToLobby, yourprofile;
	private JSplitPane splitPane;
	private MyMapPanel myMap;
	private MyTextPanel myText;
	
	private MyInfoPanel infoPanel;
	private MyOrderPanel orderPanel;
	private MyHistoryPanel historyPanel;
	
	private ArrayList<MyTerritoryPanel> TerritoryGrid;
 	
	private Map map;
	private Player player;
	
	public MainFrame(/*Map _map, Player _player*/) {
		super("Pair GUI");
		//map = _map;
		//player = _player;
		initComponents();
	}
	
	private void initComponents() {
		getContentPane().setLayout(new BorderLayout());
		createMenuBar();
		createSplitPane();
		setSize(1368,1024);
		setVisible(true);
	}
	
	private void createMenuBar() {
		menuBar = new JMenuBar();
		gameMenu = new JMenu("Game");
		menuBar.add(gameMenu);
		
		logOut = new JMenuItem(new AbstractAction("Log Out") {
			public void actionPerformed(ActionEvent ae) {
	           //return to login
				//int n = JOptionPane.showOptionDialog(this,
					   // "Are you sure you want to log out?",
					  //  "Warning",
					  //  JOptionPane.YES_NO_CANCEL_OPTION,
					  //  JOptionPane.QUESTION_MESSAGE
					  //  );
			}
		});
		
		returnToLobby = new JMenuItem(new AbstractAction("Return to Lobby") {
			public void actionPerformed(ActionEvent ae) {
				//actually return to lobby
				//int n = JOptionPane.showOptionDialog(this,
				//	    "Are you sure you want to return to lobby?",
				//	    "Warning",
				//	    JOptionPane.YES_NO_CANCEL_OPTION,
				//	    JOptionPane.QUESTION_MESSAGE
				//	    );
			}
		});
		
		gameMenu.add(returnToLobby);
		gameMenu.add(logOut);
		
		profileMenu = new JMenu("Profile");
		menuBar.add(profileMenu);
		
		yourprofile = new JMenuItem(new AbstractAction("Your Profile") {
			public void actionPerformed(ActionEvent ae) {
				JOptionPane.showMessageDialog(null, "You didn't have anything here yet.");
			}
		});
		
		profileMenu.add(yourprofile);
		
		helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);
		
		about = new JMenuItem(new AbstractAction("About") {
			public void actionPerformed(ActionEvent ae) {
				JOptionPane.showMessageDialog(null, "Pair GUI made on 2/23/14 as an example");
			}
		});
		
		helpMenu.add(about);
		setJMenuBar(menuBar);
		

		
	}
	
	private void createSplitPane() {
		//MainGUI test = new MainGUI(new Client());//maybe take out the MyMapPanel and MyInfoPanel
		//classes and make them inner classes of this one, or separate classes by themselves
		myText = new MyTextPanel();
		myMap = new MyMapPanel(5, 5);
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, myMap, myText);
		splitPane.setDividerLocation(650);
		splitPane.setOneTouchExpandable(true);
		add(splitPane);
	}
	
	private void setupGame() {
		//Setup Connection to the actual game server
	}
	
	public class MyTextPanel extends JPanel{

		public MyTextPanel(){
			GridLayout PanelLayout = new GridLayout(3, 1);	   
		  	PanelLayout.setVgap(10);	   
		  	PanelLayout.setHgap(10);
			this.setLayout(PanelLayout);
		   
		  infoPanel = new MyInfoPanel();
		  add(infoPanel);
		  historyPanel = new MyHistoryPanel();
		  orderPanel = new MyOrderPanel(historyPanel);
		  add(orderPanel);
		  add(historyPanel);
		  
	  }
	}

	public class MyMapPanel extends JPanel{
         int row, column;
         
	     public MyMapPanel(int _row, int _column/*Map _mymap*/){	   
	  	   row = _row;
	  	   column = _column;
	  	   
	       setLayout(new GridLayout(row, column, 2, 2));	   
	  	   setBorder (BorderFactory.createEtchedBorder ());
	  	   
		     
	  	   //mymap = _mymap;
	  	   TerritoryGrid = new ArrayList<MyTerritoryPanel>();
		       
	  	   for (int i = 0; i < row; i++)
	  		   for (int j = 0; j < column; j++){
	   			   MyTerritoryPanel t = new MyTerritoryPanel(new Territory(i, j), orderPanel, this);
				   TerritoryGrid.add(t);
	  			   this.add(t);
	  		   } 
	     }
	     
	     public void clearBorder(){
	    	 for (MyTerritoryPanel tp : TerritoryGrid)
	    	 tp.setBorder(BorderFactory.createEmptyBorder());
	     }
	}

	public static void main(String[] args) {
		MainFrame mf = new MainFrame();
	}
}